package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.ConfigValue;
import com.itheima.ydd.entity.Config;
import com.itheima.ydd.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yan.zhou
 * @date 2022/2/16
 */
@RestController
@Slf4j
@RequestMapping("sys_config")
public class ConfigController {
    /**
     * update   POST    已测
     * all      GET     已测
     */
    @Autowired
    private ConfigService configService;

    @PostMapping("add")
    public ResultData add(@RequestBody Config config){
        log.info("增加系统设置,{}",config);
        configService.save(config);
        return ResultData.ok("增加系统成功","");
    }

    @GetMapping("all")
    public ResultData all() {
        log.info("查看系统设置");
        Map<String, Object> map = configService.all();
        return ResultData.ok("", map);
    }
    @PostMapping("update")
    public ResultData update(@RequestBody List<ConfigValue> list){
        log.info("修改配置数据:{}",list);
        for (ConfigValue configValue : list) {
            Config config = new Config();
            config.setAttr_key(configValue.getAttr_key());
            config.setAttr_value(configValue.getAttr_value());
            config.setId(configValue.getId());
            configService.updateById(config);
        }
        return ResultData.ok("修改配置成功","");
    }
}